import hypermedia.video.*;
//import java.util.Arrays;

OpenCV opencv;
PImage img;
float threshold = 80f;
PImage changed;
PImage edgeImg;
//PImage noAlpha;
/*ColorRange[] rainbow = {new ColorRange(356, 360, 0), //other part of red
                        new ColorRange(0, 20, 0), //reds
                        new ColorRange(21, 50, 35), //orange
                        new ColorRange(51, 80, 65), //yellows
                        new ColorRange(81, 140, 111), //greens
                        new ColorRange(141, 200, 171), //cyans
                        new ColorRange(201, 240, 222), //blues
                        new ColorRange(241, 320, 280), //purples
                        new ColorRange(321, 355, 336)}; //pinks*/

void setup() {
  size( 640, 480 );
  opencv = new OpenCV( this );
  //opencv.movie( "clip1.mov", width/2, height/2 );    // load movie file
  opencv.capture(width, height);
  //noLoop();
}

void draw() {
  opencv.read();  // read a new frame and puts into the buffer //processing can go after this
  //image(opencv.image(),0,0,640,480);
  
  //RGBtoHSB(opencv.image());

  opencv.blur(opencv.MEDIAN,5);
  //runs the edge detector and draws the edges
  edgy(opencv.image());
  opencv.restore();
  //opencv.convert(opencv.GRAY);
  for(int a = 0; a < 25; a++)
    opencv.blur(opencv.BILATERAL,21,21,0,0);
  
  
  //just draws edges in color
  //opencv.copy(noAlpha);
  
  //opencv.blur(opencv.GAUSSIAN, 3);
  image(opencv.image(),0,0,640,480);
  image(edgeImg, 0, 0, 640, 480);
  //blobby();
  g.removeCache(edgeImg);
}

void mouseDragged() {
  threshold = map(mouseX,0,width,0,255);
  println( "threshold\t-> "+threshold );
}

//you can scrub through the video using mouse presses
void mousePressed() {
  float time = mouseX/float(width);
  //opencv.jump( time );            // jump to a specified frame
}


void edgy(PImage img){
  img.loadPixels();
  
  // Create an opaque image of the same size as the original
  edgeImg = createImage(img.width, img.height, ALPHA);
  //noAlpha = createImage(img.width, img.height, GRAY);
  
  //Uncomment this and the code below to see JUST the black edge detected lines on a yellow background
   //background(255, 204, 0); 
   
  // Loop through every pixel in the image.
  for (int y = 1; y < img.height; y++) { // Skip top and bottom edges
    for (int x = 1; x < img.width; x++) { // Skip left and right edges
    
       // Pixel location and color
      int loc = x + y*img.width;
      color pix = img.pixels[loc];
      
      // Pixel to the left location and color
      int leftLoc = (x - 1) + y*img.width;
      color leftPix = img.pixels[leftLoc];
      
      //Top px location and color
      int topLoc = x + (y-1)*img.width;
      color topPix = img.pixels[topLoc];
      
      // New color is difference between pixel and left neighbor
      float diff2 = abs(brightness(pix) - brightness(topPix));
      float diff = abs(brightness(pix) - brightness(leftPix));
      diff = Math.max(diff, diff2);
      
      //this draws the edge detected lines in BLACK to a new image
        edgeImg.pixels[loc] = color(0, 0, 0, diff*diff); 
    }
  }
  // State that there are changes to edgeImg.pixels[]
  edgeImg.updatePixels();
   // Uncomment this if you want to see JUST the edge detected lines
  //image(edgeImg, 0, 0, 640, 480);
}





//==================================================================================================================

/*void blobby(){
   //  opencv.blur(opencv.GAUSSIAN, 5); //prev 21
   opencv.remember();
   opencv.brightness( 10 );
   //int c = (int) map(mouseX, 0, width, -128, 128);
   int c = 121;
   opencv.contrast( c ); //prev 100
   //println(c);
    // find blobs
  Blob[] blobs = opencv.blobs( 10, opencv.image().width*opencv.image().height, 200, true, OpenCV.MAX_VERTICES*4 ); //prev 100
  println(blobs.length);

    // draw blob results
    //noFill(); //makes sure objects drawn are not filled
    //background(126);
    stroke(255,0,0);
  for( int i=0; i<blobs.length; i++ ) {
    //centroid
    color avg = opencv.image(opencv.MEMORY).get(blobs[i].centroid.x, blobs[i].centroid.y); //want to get px at centroid
    //println(blobs[i].centroid.x +" "+ blobs[i].centroid.y);
    //fill(avg);
      beginShape();
      for( int j=0; j<blobs[i].points.length; j++ ) {
          vertex( blobs[i].points[j].x, blobs[i].points[j].y );
      }
    endShape(CLOSE);
  }
}


void RGBtoHSB(PImage img){
  img.loadPixels();
  
  //copies the pixel array of the original picture into a new array
  int[] colors = Arrays.copyOf(img.pixels, img.pixels.length);
  int[] hues = new int[img.pixels.length];
  
  //sets colorMode of entire app to HSB
  colorMode(HSB,360,100,100);
  
  //iterates through every pixel in the image
    for (int y = 0; y < img.height; y++) {
      for (int x = 0; x < img.width; x++) {
        
        //extracts the color of each pixel and pulls out the hue
        color c = colors[y*img.width+x];
        hues[y*img.width+x] = (int)(hue(c));
      }
    }
    
    hues = cthreshold(hues);
    PImage himg = createImage(img.width, img.height, RGB);
    himg.loadPixels();
    for(int c = 0; c < hues.length; c++){
      himg.pixels[c] = color(hues[c], saturation(colors[c]), brightness(colors[c]));
    }
    himg.updatePixels();
    
    //resets colorMode to RGB
    colorMode(RGB,255,255,255);
    image(himg, 0, 0, 640, 480); // Draw the new image
}



int[] cthreshold(int[] hues){
  //loop through each pixel's hue
  for(int a = 0; a < hues.length; a++){
    //then through every color range we have
    for(ColorRange range: rainbow){
      
      //if it's within a range, then set it to be middle color and move onto next px
      if(range.inRange(hues[a])){
        hues[a] = range.repColor;
        break;
      }
    } 
  }
  return hues;
}*/
